package com.flyoo.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@SpringBootApplication
public class CodeGeneratorApplication implements CommandLineRunner {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    public static void main(String[] args){
        SpringApplication.run(CodeGeneratorApplication.class, args);
    }

    @Autowired
    ProjectConfig projectConfig;
    @Value("${spring.datasource.druid.url}")
    String url;
    @Value("${spring.datasource.druid.username}")
    String username;
    @Value("${spring.datasource.druid.password}")
    String password;
    @Value("${spring.datasource.druid.driver-class-name}")
    String driverClassName;

    @Override
    public void run(String... args) throws Exception {
        // 声明代码生成器对象，
        AutoGenerator mpg=new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectConfig.getOutputDir());
        gc.setAuthor(projectConfig.getAuthor());
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setEntityName("%sDO");
        gc.setControllerName("%sApiController");
        gc.setFileOverride(true);
//        gc.setSwagger2(true); //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverClassName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(projectConfig.getModuleName());
        pc.setParent(projectConfig.getParentName());
        pc.setService("dao");
        pc.setServiceImpl("dao.impl");
        mpg.setPackageInfo(pc);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass(projectConfig.getSuperEntityClass());
        strategy.setEntityBuilderModel(true);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        strategy.setSuperControllerClass(projectConfig.getSuperControllerClass());
        // 写于父类中的公共字段
        strategy.setInclude(projectConfig.getIncludeTables());
        strategy.setControllerMappingHyphenStyle(true);
        // 干掉表前缀t_
        strategy.setTablePrefix(projectConfig.getTablePrefix());
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
}
